﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 药库盘点单录入 : Form
    {
        public  int pddh = 1;//盘点单号
        public  int rkdh = 1;//入库单号
        public  int ckdh = 1;//出库单号
        public int p0_flag = 0;//盘点单flag
        public bool is_new = true;//是否为新单的标志
        DataTable dt = new DataTable();
        public string cmdtext9 = "  where   yp.bm in ( '0'";
        

        public 药库盘点单录入(int dh)
        {
            pddh = dh;
            //入库单号
            try
            {

                string cmdtext3 = "select rkdh from  yk_rkd0  order by rkdh desc ";
                SqlConnection cn1 = new SqlConnection(SQL.conStr);
                {
                    cn1.Open();
                    SqlCommand com1 = new SqlCommand(cmdtext3, cn1);
                    rkdh = (int)com1.ExecuteScalar() + 1;
                }

            }
            catch { }
            //出库单号
            try
            {

                string cmdtext4 = "select ckdh from  yk_ckd0  order by ckdh desc ";
                SqlConnection cn1 = new SqlConnection(SQL.conStr);
                {
                    cn1.Open();
                    SqlCommand com2 = new SqlCommand(cmdtext4, cn1);
                    ckdh = (int)com2.ExecuteScalar() + 1;
                }
            }
            catch { }
                        
            InitializeComponent();
            fresh();
        }

        public void fresh()//刷新
        {
            dt.Rows.Clear();
            dataGridView3.DataSource = dt;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext1 = "select  pddh as 盘点单号,(select  convert(datetime,rq-2)) as 盘点日期," +
                                "lrry as 录入,shry as 审核,jzry as 记账,bz 备注, flag as 状态,rkdh as 入库单号,ckdh as 出库单号  FROM YK_PDD0    " +
                                "WHERE pddh= " + pddh;
                SqlCommand com = new SqlCommand(cmdtext1, cn);
                SqlDataReader dr = com.ExecuteReader();
                textBox1.Text = pddh.ToString();
                if (dr.Read())
                {
                    dateTimePicker1.Text = dr["盘点日期"].ToString();
                    textBox7.Text = dr["录入"].ToString();
                    textBox9.Text = dr["审核"].ToString();
                    textBox10.Text = dr["记账"].ToString();
                    textBox8.Text = dr["备注"].ToString();
                    p0_flag = System.Convert.ToInt32(dr["状态"]);
                    try
                    {
                        rkdh = System.Convert.ToInt32(dr["入库单号"]);
                        ckdh = System.Convert.ToInt32(dr["出库单号"]);
                    }
                    catch  {}
                    is_new = false;
                    this.dataGridView1.AllowUserToAddRows = false;

                }
                else
                {
                    this.dataGridView1.AllowUserToAddRows = true;
                    is_new = true;
                }
                dr.Close();
                string cmdtext2 = "select p1.ypbm as bm,yp.mc,yp.gg,yp.bzdw,yp.bzdj,p1.kcsl,p1.kcje,p1.zmsl,p1.zmje,p1.pdsy,p1.pdsl as pysl,p1.pdje  as pyje  " +
                                   "   from yk_pdd1 p1 left join zd_yp yp on yp.bm=p1.ypbm   " +
                                    "   where p1.pddh="+pddh+" order by ypbm";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext2, cn);
                da.Fill(dt);
                dataGridView3.DataSource = dt;
            }

            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView3.MultiSelect = false;


            if (!is_new)
            {
                textBox2.Text = dataGridView3.Rows.Count.ToString();
                decimal textBox3_Text = 0;
                decimal textBox5_Text = 0;
                for (int index = 0; index < dataGridView3.Rows.Count-1; index++)
                {
                    textBox3_Text = (decimal)dataGridView3.Rows[index].Cells["实际金额"].Value + textBox3_Text;
                    textBox5_Text = (decimal)dataGridView3.Rows[index].Cells["账面金额"].Value + textBox5_Text;
                }
                textBox3.Text = textBox3_Text.ToString();
                textBox5.Text = textBox5_Text.ToString();
            }
            else
            {
                
            }
            selectRow();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            装入药品.cmdtext1 = "";
            this.Close();
        }

        public void selectRow()
        {
            if (p0_flag == 19)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
                button3.Enabled = false;
                button8.Enabled = false;
                button11.Enabled = false;
               
            }
            else if (p0_flag == 17)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = true;
                button3.Enabled = false;
                button8.Enabled = false;
                button11.Enabled = false;
            }
            else if (p0_flag == 16)
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button5.Enabled = false;
                button6.Enabled = false;
                button3.Enabled = true;
                button8.Enabled = true;
                button11.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)//审核
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                             "UPDATE YK_PDD0 SET " +
                             "SHRY = '" + LoginForm.username + "', " +
                             "JZRY = '', " +
                             "FLAG = 17  " +
                             "WHERE PDDH = '" + pddh+ "'";


            cmdtext3 = cmdtext3 + "   UPDATE YK_RKD0 SET " +
                                  "SHRY = '" + LoginForm.username + "', " +
                                  "JZRY = '', " +
                                  "FLAG = 17  " +
                                 "  WHERE RKDH = '" + rkdh + "'";
            cmdtext3 = cmdtext3 + "   UPDATE YK_CKD0 SET " +
                                  "SHRY = '" + LoginForm.username + "', " +
                                  "JZRY = '', " +
                                  "FLAG = 17  " +
                                 "  WHERE CKDH = '" + ckdh + "'" +
                                 "    COMMIT TRANSACTION ";

            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button5_Click(object sender, EventArgs e)//消审
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                                 "UPDATE YK_PDD0 SET " +
                                 "SHRY = '', " +
                                 "JZRY = '', " +
                                 "FLAG = 16  " +
                                 "WHERE PDDH = '" + pddh + "'";

            cmdtext3 = cmdtext3 + "   UPDATE YK_RKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                                 "  WHERE RKDH = '" + rkdh + "'";
            cmdtext3 = cmdtext3 + "   UPDATE YK_CKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                                 "  WHERE CKDH = '" + ckdh + "'" +
                                 "    COMMIT TRANSACTION ";

           
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button6_Click(object sender, EventArgs e)//记账
        {
            int jzrq = 0;//记账日期
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext4 = "select  convert(int,(select Convert(DateTime,getdate())))+1";
                SqlCommand cmd = new SqlCommand(cmdtext4, cn1);
                jzrq = (int)cmd.ExecuteScalar();
            }
            SqlConnection cn2 = new SqlConnection(SQL.conStr);
            {
                cn2.Open();
                string cmdtext3 = "BEGIN TRANSACTION  " +
                                  "   UPDATE YK_PDD0 SET " +
                                  "JZRY = '" + LoginForm.username + "', " +
                                  "FLAG = 19,  " +
                                  "jzrq =" + jzrq +
                                  "  WHERE pddh = '" + pddh + "'";

                cmdtext3 = cmdtext3 + "   UPDATE YK_RKD0 SET " +
                                     "JZRY = '" + LoginForm.username + "', " +
                                     "FLAG = 19,  " +
                                     "jzrq =" + jzrq +
                                     "  WHERE RKDH = '" + rkdh+ "'";
                cmdtext3 = cmdtext3 + "   UPDATE YK_CKD0 SET " +
                                     "JZRY = '" + LoginForm.username + "', " +
                                     "FLAG = 19,  " +
                                     "jzrq =" + jzrq +
                                     "  WHERE CKDH = '" + ckdh + "'" +
                                     "    COMMIT TRANSACTION ";


                string cmd_insert = "INSERT INTO YP_CRKJZ SELECT YPBM,-SL FROM YK_CKD1 where CKDH='" + ckdh + "'";
                cmd_insert = cmd_insert + "  INSERT INTO YP_CRKJZ SELECT YPBM,SL FROM YK_RKD1 WHERE RKDH='" + rkdh + "'";

                SqlCommand cmd1 = new SqlCommand(cmd_insert, cn2);
                cmd1.ExecuteNonQuery();
                SqlCommand cmd = new SqlCommand(cmdtext3, cn2);
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("记账成功");
            fresh();
        }


        private void button1_Click(object sender, EventArgs e)//保存
        {
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            int dpt_1 = 0;
            string cmdtext6 = "select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2";
            SqlConnection cn1 = new SqlConnection(SQL.conStr);//盘点日期
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    dpt_1 = (int)read[0];
                }
                read.Close();
                cn1.Close();
            }


            if (is_new)
            {
                string cmdtext3 = "  INSERT INTO YK_PDD0(YKBM,pddh,RQ,LRRY,SHRY,JZRY,FLAG,BZ)" +
                                  "  VALUES(  '401', " + textBox1.Text + ", " + dpt_1 + ", '" + LoginForm.username + "', '', '', 16, '" + textBox8.Text + "')   ";

                for (int index = 0; index < dataGridView3.Rows.Count; index++)
                {
                    try
                    {
                        if (System.Convert.ToInt32(dataGridView3.Rows[index].Cells["盘盈数量"].Value) > 0)
                            cmdtext3 = cmdtext3 + "  insert  into YK_PDD1(pddh,ypbm,kcsl,kcje,zmsl,zmje,pdsl,pdje,pdsy) values (" +
                                textBox1.Text + ",'" + dataGridView3.Rows[index].Cells["药品编码"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际金额"].Value.ToString() +
                                "','" + dataGridView3.Rows[index].Cells["账面数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["账面金额"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["盘盈数量"].Value.ToString() +
                                "','" + dataGridView3.Rows[index].Cells["盘盈金额"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["损益"].Value.ToString() + "')";
                        else if (System.Convert.ToInt32(dataGridView3.Rows[index].Cells["盘亏数量"].Value) >= 0)
                            cmdtext3 = cmdtext3 + "  insert  into YK_PDD1(pddh,ypbm,kcsl,kcje,zmsl,zmje,pdsl,pdje,pdsy) values (" +
                                textBox1.Text + ",'" + dataGridView3.Rows[index].Cells["药品编码"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际金额"].Value.ToString() +
                                "','" + dataGridView3.Rows[index].Cells["账面数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["账面金额"].Value.ToString() + "','-" + dataGridView3.Rows[index].Cells["盘亏数量"].Value.ToString() +
                                "','-" + dataGridView3.Rows[index].Cells["盘亏金额"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["损益"].Value.ToString() + "')";
                    
                    }
                    catch { }
                }

                SqlConnection cn3 = new SqlConnection(SQL.conStr);
                {
                    cn3.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn3);
                    com.ExecuteNonQuery();
                }

            }
            else
            {

                string cmdtext3 = " update YK_PDD0 set RQ=" + dpt_1 + " ,BZ='" + textBox8.Text + "'   where pddh= " + pddh;
                cmdtext3 = cmdtext3 + "   delete   from yk_pdd1  where pddh= " + pddh;
                cmdtext3 = cmdtext3 + "   delete   from yk_rkd1  where rkdh= " + rkdh;
                cmdtext3 = cmdtext3 + "   delete   from yk_ckd1  where ckdh= " + ckdh;
                for (int index = 0; index < dataGridView3.Rows.Count; index++)
                {
                    try
                    {
                        if (System.Convert.ToInt32(dataGridView3.Rows[index].Cells["盘盈数量"].Value) > 0)
                            cmdtext3 = cmdtext3 + "  insert  into YK_PDD1(pddh,ypbm,kcsl,kcje,zmsl,zmje,pdsl,pdje,pdsy) values (" +
                                textBox1.Text + ",'" + dataGridView3.Rows[index].Cells["药品编码"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际金额"].Value.ToString() +
                                "','" + dataGridView3.Rows[index].Cells["账面数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["账面金额"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["盘盈数量"].Value.ToString() +
                                "','" + dataGridView3.Rows[index].Cells["盘盈金额"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["损益"].Value.ToString() + "')";
                        else if (System.Convert.ToInt32(dataGridView3.Rows[index].Cells["盘亏数量"].Value) >= 0)
                            cmdtext3 = cmdtext3 + "  insert  into YK_PDD1(pddh,ypbm,kcsl,kcje,zmsl,zmje,pdsl,pdje,pdsy) values (" +
                                textBox1.Text + ",'" + dataGridView3.Rows[index].Cells["药品编码"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["实际金额"].Value.ToString() +
                                "','" + dataGridView3.Rows[index].Cells["账面数量"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["账面金额"].Value.ToString() + "','-" + dataGridView3.Rows[index].Cells["盘亏数量"].Value.ToString() +
                                "','-" + dataGridView3.Rows[index].Cells["盘亏金额"].Value.ToString() + "','" + dataGridView3.Rows[index].Cells["损益"].Value.ToString() + "')";
                    }
                    catch { }
                }
                SqlConnection cn4 = new SqlConnection(SQL.conStr);
                {
                    cn4.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn4);
                    com.ExecuteNonQuery();
                }
            }

            //盘盈盘亏单
            string cmdtext4 = "";
            cmdtext4 = cmdtext4 + "  insert into yk_rkd0 (rkdh,flag,ykbm,rklx,rq) values (" + rkdh + ",16,'401',5," + dpt_1 + ")";
            cmdtext4 = cmdtext4 + "  insert into yk_ckd0 (ckdh,flag,ykbm,cklx,rq) values (" + ckdh + ",16,'401',15," + dpt_1 + ")";
            for (int index = 0; index < dataGridView3.Rows.Count; index++)
            {
                try
                {
                    if (System.Convert.ToInt32(dataGridView3.Rows[index].Cells["盘盈数量"].Value) > 0)
                        cmdtext4 = cmdtext4 + "  insert into yk_rkd1(rkdh,ypbm,jj,lsj,cklsj,sl,sysl,je,jjje,ljje)  values (" +
                            rkdh + ",'" + dataGridView3.Rows[index].Cells["药品编码"].Value.ToString() + "',0," + dataGridView3.Rows[index].Cells["单价"].Value.ToString() +
                            "," + dataGridView3.Rows[index].Cells["单价"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["盘盈数量"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["盘盈数量"].Value.ToString() +
                            "," + dataGridView3.Rows[index].Cells["盘盈金额"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["盘盈金额"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["盘盈金额"].Value.ToString() + ")";
                    else if (System.Convert.ToInt32(dataGridView3.Rows[index].Cells["盘亏数量"].Value) > 0)
                        cmdtext4 = cmdtext4 + "  insert into yk_ckd1(ckdh,ypbm,sl,dj,je,cbdj,cbje)  values (" +
                             ckdh + ",'" + dataGridView3.Rows[index].Cells["药品编码"].Value.ToString() + "'," + dataGridView3.Rows[index].Cells["盘亏数量"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["单价"].Value.ToString() +
                             "," + dataGridView3.Rows[index].Cells["盘亏金额"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["单价"].Value.ToString() + "," + dataGridView3.Rows[index].Cells["盘亏金额"].Value.ToString() + ")";
                }
                catch { }
            }

            cmdtext4 = cmdtext4 + "  delete from yk_rkd0 where rkdh in (select rkdh from yk_rkd0  where rkdh not in(select rkdh from yk_rkd1))";
            cmdtext4 = cmdtext4 + "  delete from yk_ckd0 where ckdh in (select ckdh from yk_ckd0  where ckdh not in(select ckdh from yk_ckd1))";
            cmdtext4 = cmdtext4 + "  update yk_pdd0 set rkdh=(select top 1 rkdh from yk_rkd0 where rkdh="+rkdh+") ,ckdh=(select top 1 ckdh from yk_ckd0 where ckdh="+ckdh+")  where pddh="+pddh;

            try
            {
                SqlConnection cn5 = new SqlConnection(SQL.conStr);
                {
                    cn5.Open();
                    SqlCommand com = new SqlCommand(cmdtext4, cn5);
                    com.ExecuteNonQuery();
                }
            }
            catch { }

            is_new = false;
            MessageBox.Show("保存成功");
        }

        private void 药库入库单录入_Load(object sender, EventArgs e)
        {
            fresh();
            this.dataGridView3.MultiSelect = false;
            dataGridView4.Visible = false;
            this.dataGridView4.AllowUserToAddRows = false;
            this.dataGridView4.MultiSelect = false;
            this.dataGridView4.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView4.ReadOnly = true;
            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
        }


        private void button3_Click(object sender, EventArgs e)//装入药品
        {
            dt.Rows.Clear();
            dataGridView3.DataSource = dt;
            string cmdtext1 = "select  yp.bm,mc,gg,bzdw,bzdj  from zd_yp yp   ";
            Form f = new Form();
            f = new 装入药品();
            if (f.ShowDialog() == DialogResult.OK)
            {
                cmdtext1 = cmdtext1 + cmdtext9 + ")  " + 装入药品.cmdtext() + "  order by yp.bm";


                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);

                    da.Fill(dt);
                    dataGridView3.DataSource = dt;
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)//读取账面数
        {
            dt.Rows.Clear();
            dataGridView3.DataSource = dt;
            string cmdtext2 = "select yp.bm,yp.mc,yp.gg,yp.bzdw,yp.bzdj ,  " +
                " (select  sum(r1.sysl) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm ) as zmsl , " +
                "   ((select  sum(r1.je) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm )-isnull((select  sum(c1.je) from yk_ckd1 c1 where c1.ypbm=yp.bm and c1.ckdh in (select ckdh from yk_ckd0 c0 where c0.flag=19) group by c1.ypbm ),0))as zmje ,  " +
                "  (select  sum(r1.sysl) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm )as pksl ,    " +
                " yp.bzdj*(select  sum(r1.sysl) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm )as pkje " +
                "   from zd_yp yp   " + cmdtext9 + ")  " + 装入药品.cmdtext() +
                "   order by bm   ";

            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                SqlDataAdapter da = new SqlDataAdapter(cmdtext2, cn);
                da.Fill(dt);
                dataGridView3.DataSource = dt;
            }
            for (int index = 0; index < dataGridView3.Rows.Count; index++)
            {
                try
                {
                    dataGridView3.Rows[index].Cells["损益"].Value = System.Convert.ToDecimal(dataGridView3.Rows[index].Cells["盘亏金额"].Value) -System.Convert.ToDecimal(dataGridView3.Rows[index].Cells["账面金额"].Value);
                }
                catch { }
            }
        }

        private void button8_Click(object sender, EventArgs e)//复制账面数
        {
            dt.Rows.Clear();
            dataGridView3.DataSource = dt;

            string cmdtext2 = "select yp.bm,yp.mc,yp.gg,yp.bzdw,yp.bzdj ,  " +
                " (select  sum(r1.sysl) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm ) as zmsl , " +
                "   ((select  sum(r1.je) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm )-isnull((select  sum(c1.je) from yk_ckd1 c1 where c1.ypbm=yp.bm and c1.ckdh in (select ckdh from yk_ckd0 c0 where c0.flag=19) group by c1.ypbm ),0))as zmje ,  " +
                "  (select  sum(r1.sysl) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm )as kcsl ,    " +
                "((select  sum(r1.je) from yk_rkd1 r1 where r1.ypbm=yp.bm and r1.rkdh in (select rkdh from yk_rkd0 r0 where r0.flag=19) group by r1.ypbm )-isnull((select  sum(c1.je) from yk_ckd1 c1 where c1.ypbm=yp.bm and c1.ckdh in (select ckdh from yk_ckd0 c0 where c0.flag=19) group by c1.ypbm ),0))as kcje " +
                "   from zd_yp yp     " + cmdtext9 + ")  " + 装入药品.cmdtext() +
                "   order by bm   ";


            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                SqlDataAdapter da = new SqlDataAdapter(cmdtext2, cn);
                da.Fill(dt);
                dataGridView3.DataSource = dt;
            }
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)//药品编码定位
        {
            string cmdtext5 = "select bm 编码,MC AS 名称 from zd_yp where dh like '" + textBox6.Text.ToString() + "%' or bm like '" + textBox6.Text.ToString() + "%' or mc like '" + textBox6.Text.ToString() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext5, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView4.DataSource = dt;
                dataGridView4.Visible = true;
            }
        }

        private void dataGridView4_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            select_bm();
        }

        public void select_bm()//药品编码定位下拉框
        {
            bool bool1 = true;
            for (int index = 0; index < dataGridView3.RowCount; index++)
            {
                try
                {
                    if (dt.Rows[index][0].ToString() == dataGridView4.CurrentRow.Cells["编码"].Value.ToString().Trim())
                    {
                        this.dataGridView3.Rows[index].Selected = true;
                        dataGridView3.FirstDisplayedScrollingRowIndex = index;
                        bool1 = false;
                        break;
                    }
                }
                catch { }
            }
            if (bool1)
            {
                string cmdtext6 = "select  bm,mc,gg,bzdw,bzdj  from zd_yp   where  dh = '" + dataGridView4.CurrentRow.Cells["编码"].Value.ToString().Trim() +
                    "' or bm = '" + dataGridView4.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView4.CurrentRow.Cells["编码"].Value.ToString().Trim() + "'";
                cmdtext9 = cmdtext9 + " ,'" + dataGridView4.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmdtext6, cn);
                    da.Fill(dt);
                    dataGridView3.DataSource = dt;

                }
               
            }
            dataGridView4.Visible = false;
        }



        private void dataGridView3_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView3.CurrentCell.OwningColumn.Name == "实际数量")
            {
                try
                {
                    int i = System.Convert.ToInt32(dataGridView3.CurrentRow.Cells["损益"].Value);
                }
                catch
                {
                    try
                    {
                        Decimal pypk = System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面数量"].Value);
                        if (pypk > 0)
                        {
                            dataGridView3.CurrentRow.Cells["盘盈数量"].Value = pypk;
                            dataGridView3.CurrentRow.Cells["盘盈金额"].Value = System.Convert.ToString(pypk * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));

                            dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面金额"].Value) + System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘盈金额"].Value));
                            dataGridView3.CurrentRow.Cells["盘亏数量"].Value = 0;
                            dataGridView3.CurrentRow.Cells["盘亏金额"].Value = 0;
                            dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        }
                        else
                        {
                            dataGridView3.CurrentRow.Cells["盘亏数量"].Value = -pypk;
                            dataGridView3.CurrentRow.Cells["盘亏金额"].Value = -pypk * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value);
                            dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面金额"].Value) + System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘亏金额"].Value));
                            dataGridView3.CurrentRow.Cells["盘盈数量"].Value = 0;
                            dataGridView3.CurrentRow.Cells["盘盈金额"].Value = 0;
                            dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        }
                    }
                    catch
                    {
                        dataGridView3.CurrentRow.Cells["盘盈数量"].Value = dataGridView3.CurrentRow.Cells["实际数量"].Value;
                        dataGridView3.CurrentRow.Cells["盘盈金额"].Value = System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘盈数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value);
                        dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                    }
                    return;
                }



                if (System.Convert.ToInt32(dataGridView3.CurrentRow.Cells["损益"].Value) <= 0)//亏了,降价了
                {
                    try
                    {
                        Decimal pypk = System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面数量"].Value);
                        if (pypk > 0)
                        {
                            dataGridView3.CurrentRow.Cells["盘盈数量"].Value = pypk;
                            dataGridView3.CurrentRow.Cells["盘盈金额"].Value = System.Convert.ToString(pypk * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));

                            dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面金额"].Value) + System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘盈金额"].Value));
                            dataGridView3.CurrentRow.Cells["盘亏数量"].Value = 0;
                            dataGridView3.CurrentRow.Cells["盘亏金额"].Value = 0;
                            dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        }
                        else
                        {
                            dataGridView3.CurrentRow.Cells["盘亏数量"].Value = -pypk;
                            dataGridView3.CurrentRow.Cells["盘亏金额"].Value = -pypk * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value);
                            dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面金额"].Value) + System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘亏金额"].Value));
                            dataGridView3.CurrentRow.Cells["盘盈数量"].Value = 0;
                            dataGridView3.CurrentRow.Cells["盘盈金额"].Value = 0;
                            dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        }
                    }
                    catch
                    {
                        dataGridView3.CurrentRow.Cells["盘盈数量"].Value = dataGridView3.CurrentRow.Cells["实际数量"].Value;
                        dataGridView3.CurrentRow.Cells["盘盈金额"].Value = System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘盈数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value);
                        dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                    }
                }
                else//赚了,涨价了
                {
                    try
                    {
                        Decimal pypk = System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["账面数量"].Value);
                        if (pypk > 0)
                        {
                            dataGridView3.CurrentRow.Cells["盘盈数量"].Value = pypk;
                            dataGridView3.CurrentRow.Cells["盘盈金额"].Value = System.Convert.ToString(pypk * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));

                            dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                            dataGridView3.CurrentRow.Cells["盘亏数量"].Value = 0;
                            dataGridView3.CurrentRow.Cells["盘亏金额"].Value = 0;
                            //dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        }
                        else
                        {
                            dataGridView3.CurrentRow.Cells["盘亏数量"].Value = -pypk;
                            dataGridView3.CurrentRow.Cells["盘亏金额"].Value = -pypk * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value);
                            dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                            dataGridView3.CurrentRow.Cells["盘盈数量"].Value = 0;
                            dataGridView3.CurrentRow.Cells["盘盈金额"].Value = 0;
                            //dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        }
                    }
                    catch
                    {
                        dataGridView3.CurrentRow.Cells["盘盈数量"].Value = dataGridView3.CurrentRow.Cells["实际数量"].Value;
                        dataGridView3.CurrentRow.Cells["盘盈金额"].Value = System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["盘盈数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value);
                        dataGridView3.CurrentRow.Cells["实际金额"].Value = System.Convert.ToString(System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                        //dataGridView3.CurrentRow.Cells["损益"].Value = (System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际金额"].Value) - System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["实际数量"].Value) * System.Convert.ToDecimal(dataGridView3.CurrentRow.Cells["单价"].Value));
                    }
                }

            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            try
            {
                f = new 药库入库单录入(rkdh);
            }
            catch { MessageBox.Show("没有盘盈单"); return; }
            f.ShowDialog();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            try
            {
                f = new 药库入库单录入(rkdh);
            }
            catch { MessageBox.Show("没有盘盈单"); return; }
            f.ShowDialog();
        }



        private void button4_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            try
            {
                f = new 药品出库单录入(ckdh);
            }
            catch { MessageBox.Show("没有盘亏单"); return; }
            f.ShowDialog();
        }
    }
}